﻿using UnityEngine;
using System.Collections.Generic;
using System.Collections;
using System.IO;
using System.Xml;
using Clojure;
public class ActorManager : Singleton<ActorManager> 
{
    public  Dictionary<string, XmlActor> Actors = new Dictionary<string, XmlActor>();
    public string XmlPath;
    public void InitFormXml() 
    {
        this.DefineActorFuncs();

        XmlPath = Game.ConfigPath + "Actors.xml";
        string strUnit = File.ReadAllText(XmlPath);
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(strUnit);
        XmlNode node = doc.ChildNodes[0];
        for (int i = 0; i < node.ChildNodes.Count; i++)
        {
            XmlActor xmlActor = new XmlActor();
            xmlActor.parse(node.ChildNodes[i]);
            this.Actors[xmlActor.Name] = xmlActor;
        }
    }

    public void DefineActorFuncs()
    {
       RT.MainContext.Define("actor-event", new FunctionNode(_actor_event));
       RT.MainContext.Define("create-actor", new FunctionNode(_create_actor));
    }

    public ASTNode _actor_event(List<ASTNode> args, Context Context)
    {
        Unit unit = (Unit)((ObjectNode)Context.Find("self-unit")).Val;
        Actor actor = (Actor)((ObjectNode)Context.Find("self-actor")).Val;
        string eventName = ((StringNode)args[0]).Val;
        actor.AddEvent(eventName,args[1]);
        unit.AddEventHandler(eventName,actor);
        return null;
    }

    public ASTNode _create_actor(List<ASTNode> args, Context context)  
    {
        Actor actor = (Actor)((ObjectNode)context.Find("self-actor")).Val;
        actor.Init();
        return null;
    }


}
